package com.songqintuan.persist.service.hibernate.impl;

import java.util.HashMap;

import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.songqintuan.common.dal.hibernate.baseservice.IOperateService;
import com.songqintuan.common.dal.hibernate.baseservice.OperateServiceImp;
import com.songqintuan.model.po.Customer;
import com.songqintuan.persist.service.hibernate.ICustomerService;

public class CustomerService extends OperateServiceImp<Customer, String>
		implements IOperateService<Customer, String>,
		ICustomerService<Customer, String> {

	public int updateCustomerLoginPassword(Customer customer){
		int result = 0;
		Map criteria = new HashMap();
		String hql="update Customer cus set " + " cus.loginPassword = :loginPassword " +
				" where 1=1 and cus.id = :id ";
		criteria.put("loginPassword", customer.getLoginPassword());
		criteria.put("id", customer.getId());

		SessionFactory sf =(SessionFactory) this.getGenericDao().getHibernateTemplate().getSessionFactory();
		Session session = sf.openSession();
		Transaction tx = null;
		try {
			Query q = session.createQuery(hql);
			q.setProperties(criteria);
			tx = session.beginTransaction();
			result = q.executeUpdate();
			tx.commit();
			tx = null;
			} catch (HibernateException e) {
				e.printStackTrace();
				if (null != tx) {
					tx.rollback();
				}
			} finally {
				session.close();
			  }
		return result;
		
	}
	
	public int updateCustomerPasQueAns(String question, String answer, String id) {
		int result = 0;
		Map criteria = new HashMap();
		String hql="update Customer cus set " + " cus.passwordQuestion = :passwordQuestion " + ", cus.passwordAnswer = :passwordAnswer " +
				" where 1=1 and cus.id = :id ";
		criteria.put("passwordQuestion", question);
		criteria.put("passwordAnswer", answer);
		criteria.put("id", id);

		SessionFactory sf =(SessionFactory) this.getGenericDao().getHibernateTemplate().getSessionFactory();
		Session session = sf.openSession();
		Transaction tx = null;
		try {
			Query q = session.createQuery(hql);
			q.setProperties(criteria);
			tx = session.beginTransaction();
			result = q.executeUpdate();
			tx.commit();
			tx = null;
			} catch (HibernateException e) {
				e.printStackTrace();
				if (null != tx) {
					tx.rollback();
				}
			} finally {
				session.close();
			  }
		return result;
	}
	public int updateCustomerContact(String mobile, String phone, String fax,
			String qicq, String msn, String skype, String wangwangNum, String id){
		int result = 0;
		Map criteria = new HashMap();
		String hql="update Customer cus set " + " cus.mobile = :mobile " + 
												", cus.phone = :phone " +
												", cus.fax = :fax " +
												", cus.qicq = :qicq " +
												", cus.msn = :msn " +
												", cus.skype = :skype " +
												", cus.wangwangNum = :wangwangNum " +							
				" where 1=1 and cus.id = :id ";
		criteria.put("mobile", mobile);
		criteria.put("phone", phone);
		criteria.put("fax", fax);
		criteria.put("qicq", qicq);
		criteria.put("msn", msn);
		criteria.put("skype", skype);
		criteria.put("wangwangNum", wangwangNum);
		criteria.put("id", id);

		SessionFactory sf =(SessionFactory) this.getGenericDao().getHibernateTemplate().getSessionFactory();
		Session session = sf.openSession();
		Transaction tx = null;
		try {
			Query q = session.createQuery(hql);
			q.setProperties(criteria);
			tx = session.beginTransaction();
			result = q.executeUpdate();
			tx.commit();
			tx = null;
			} catch (HibernateException e) {
				e.printStackTrace();
				if (null != tx) {
					tx.rollback();
				}
			} finally {
				session.close();
			  }
		return result;
	}
}
